<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
    >
      <el-form-item label="企业" prop="enterpriseId">
        <el-select
          size="small"
          v-model="queryParams.enterpriseId"
          filterable
          placeholder="请选择企业"
          clearable>
          <el-option
            v-for="item in zsCustomerList"
            :key="item.id"
            :label="item.enterpriseName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="租赁记录状态" prop="recordState">
        <el-select v-model="queryParams.recordState"
                   clearable
                   placeholder="请选择状态">
          <el-option label="未到期" value="0"/>
          <el-option label="已到期" value="1"/>
        </el-select>
      </el-form-item>

      <el-form-item label="租金计费方式" prop="calculationMode">
        <el-select v-model="queryParams.calculationMode"
                   clearable
                   placeholder="请选择状态">
          <el-option label="手动计费" value="手动计费"/>
          <el-option label="自动计费" value="自动计费"/>
        </el-select>
      </el-form-item>

      <el-form-item label="生效状态" prop="effect">
        <el-select v-model="queryParams.effect"
                   clearable
                   placeholder="请选择状态">
          <el-option label="未生效" value="0"/>
          <el-option label="已生效" value="1"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['life:feLeaseRecord:add']"
          >新增</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['life:feLeaseRecord:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['life:feLeaseRecord:remove']"
        >删除
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['life:feLeaseRecord:export']"
        >导出
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="feLeaseRecordList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="企业" align="center" prop="enterpriseName"/>
      <el-table-column
        label="租赁开始时间"
        align="center"
        prop="beginTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="租赁结束时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="合同总面积(㎡)" prop="contractArea" align="center"/>
      <el-table-column label="租赁保证金(元)" prop="feSurety.surety" align="center"/>
      <el-table-column label="装修保证金(元)" prop="feSurety.decorationSurety" align="center"/>

      <el-table-column
        label="状态"
        align="center"
        prop="recordState"
        width="80"
      >
        <template slot-scope="scope">
          <span>{{
              scope.row.recordState === '0'
                ? '未到期'
                : scope.row.recordState === '1'
                ? '已到期'
                : '暂无'
            }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="是否生效"
        align="center"
        prop="recordState"
        width="80"
      >
        <template slot-scope="scope">
          <span>{{
              scope.row.effect == '0'
                ? '未生效'
                : scope.row.effect == '1'
                ? '已生效'
                : '暂无'
            }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEffect(scope.row)"
            v-hasPermi="['life:feLeaseRecord:edit']"
          >状态修改
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['life:feLeaseRecord:edit']"
          >详情
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            v-hasPermi="['life:feLeaseRecord:edit']"
          >修改
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['life:feLeaseRecord:remove']"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="effectOpen" width="500px" append-to-body>
      <el-form
        ref="form"
        size="small"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="租赁生效状态" prop="effect">
          <el-select
            clearable
            v-model="form.effect"
            placeholder="请选择租赁状态">
            <el-option label="未生效" value="0"></el-option>
            <el-option label="已生效" value="1"></el-option>
          </el-select>



        </el-form-item>

        <el-form-item label="租赁计算方式" prop="effect">
          <el-select
            clearable
            v-model="form.calculationMode"
            placeholder="请选择租赁计算方式">
            <el-option label="手动计费" value="手动计费"></el-option>
            <el-option label="自动计费" value="自动计费"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEffect">确 定</el-button>
        <el-button @click="effectCancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="billOpen" width="500px" append-to-body>
      <el-form
        ref="form"
        size="small"
        :model="form"
        :rules="rules"
        label-width="120px">
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input disabled v-model="form.enterpriseName"/>
        </el-form-item>
        <el-form-item label="账单类型" prop="billType">
          <el-select
            clearable
            v-model="form.billType"
            placeholder="请选择账单类型">
            <el-option label="办公费用" value="办公费用"></el-option>
            <el-option label="设施费用" value="设施费用"></el-option>
            <el-option label="物业费用" value="物业费用"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="账单区间段" prop="billDate">
          <el-date-picker
            v-model="form.billDate"
            size="small"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="下期账单时间" prop="billNextDay">
          <el-date-picker
            clearable
            size="small"
            v-model="form.billNextDay"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择下期账单时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="违约计算时间" prop="billDeadline">
          <el-date-picker
            clearable
            size="small"
            v-model="form.billDeadline"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择违约金收取时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="应收金额" prop="billMoney">
          <el-input v-model="form.billMoney"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitBill">确 定</el-button>
        <el-button @click="effectCancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改租赁记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        ref="form"
        size="small"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="企业" prop="enterpriseId">
          <el-select clearable
                     v-model="form.enterpriseId" filterable
                     placeholder="请选择企业"
          >
            <el-option
              v-for="item in zsCustomerList"
              :key="item.id"
              :label="item.enterpriseName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.beginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="免租开始时间" prop="rentFreeBeginDate">
          <el-date-picker
            clearable
            size="small"
            v-model="form.rentFreeBeginDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择免租开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="免租结束时间" prop="rentFreeEndDate">
          <el-date-picker
            clearable
            size="small"
            v-model="form.rentFreeEndDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择免租结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="厂房/写字楼费用" prop="officeFeeId">
          <el-input
            v-model="form.officeFeeId"
            placeholder="请输入厂房/写字楼费用Id"
          />
        </el-form-item>
        <el-form-item label="公寓费用" prop="apartmentFeeId">
          <el-input
            v-model="form.apartmentFeeId"
            placeholder="请输入公寓费用Id"
          />
        </el-form-item>
        <el-form-item label="物业费用" prop="propertyFeeId">
          <el-input
            v-model="form.propertyFeeId"
            placeholder="请输入物业费用Id"
          />
        </el-form-item>
        <el-form-item label="设施费用" prop="facilityFeeId">
          <el-input
            v-model="form.facilityFeeId"
            placeholder="请输入设施费用Id"
          />
        </el-form-item>
        <el-form-item label="固定费用" prop="fixedFeeId">
          <el-input v-model="form.fixedFeeId" placeholder="请输入固定费用Id"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="停车费用" prop="parkingFeeId">
          <el-input
            v-model="form.parkingFeeId"
            placeholder="请输入停车费用Id"
          />
        </el-form-item>
        <el-form-item label="租赁记录类型" prop="recordType">
          <el-select
            clearable
            v-model="form.recordType"
            placeholder="请选择租赁记录类型"
          >
            <el-option label="企业入驻" value="企业入驻"></el-option>
            <el-option label="房源租赁" value="房源租赁"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电梯费用Id" prop="elevatorFeeId">
          <el-input
            v-model="form.elevatorFeeId"
            placeholder="请输入电梯费用Id"
          />
        </el-form-item>
        <el-form-item label="租赁记录状态" prop="recordState">
          <el-select
            clearable
            v-model="form.recordState"
            placeholder="请选择租赁记录状态"
          >
            <el-option label="未到期" value="未到期"></el-option>
            <el-option label="已到期" value="已到期"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房屋id(历史使用)" prop="roomIds">
          <el-input
            v-model="form.roomIds"
            placeholder="请输入房屋id(历史使用)"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFeLeaseRecord,
  getFeLeaseRecord,
  delFeLeaseRecord,
  addFeLeaseRecord,
  updateFeLeaseRecord, updateEffect, createBill
} from '@/api/smartParks/operatingFee/leasing'
import { listQyEnterprise } from '@/api/smartParks/merchants/merchantsCompanySettlein'
import { getOpmEnvAccident } from '@/api/smartParks/environment/opmEnvAccident'
import { addAfElevator, updateAfElevator } from '@/api/smartParks/security/securityElevator'

export default {
  name: 'Leasing',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租赁记录表格数据
      feLeaseRecordList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      effectOpen: false,
      billOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseId: null,
        recordState: null,
        calculationMode: null,
        effect: null
      },
      zsCustomerList: [],
      // 表单参数
      form: {},
      feApartmentList: [], //公寓
      feFixedList: [], //固定费用
      fePropertyList: [], //物业费用
      feElevatorList: [], //电梯费用
      feFacilityList: [], //设施
      feOfficeList: [], //办公
      // 表单校验
      rules: {
        billDeadline: [
          { required: true, message: '违约时间不能为空', trigger: 'blur' }
        ],
        billMoney: [
          { required: true, message: '账单金额不能为空', trigger: 'blur' }
        ],
        billDate: [
          { required: true, message: '账单区间不能为空', trigger: 'blur' }
        ],
        billType: [
          { required: true, message: '账单类型不能为空', trigger: 'blur' }
        ],
        billNextDay: [
          { required: true, message: '下一期账单不能为空', trigger: 'blur' }
        ]
      },
      flag: true,
      effect: null
    }
  },
  beforeRouteEnter(to, from, next) {
    if (to.query.id && from.name === 'MerchantsCompanySettlein') {
      next((vm) => {
        vm.queryParams.enterpriseId = to.query.id
        vm.getList()
      })
    } else {
      next((vm) => {
        vm.queryParams.enterpriseId = ''
        vm.getList()
      })
    }
  },
  created() {
    listQyEnterprise().then((response) => {
      this.zsCustomerList = response.rows
    })
  },
  methods: {
    /** 查询租赁记录列表 */
    getList() {
      this.loading = true
      listFeLeaseRecord(this.queryParams).then((response) => {
        this.feLeaseRecordList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    effectCancel() {
      this.effectOpen = false
      this.reset()
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        billType: null,
        billMoney: null,
        billNextDay: null,
        billDeadline: null,
        billDate: [],
        billBeginDate: null,
        billEndDate: null,
        id: null,
        enterpriseId: null,
        beginTime: null,
        endTime: null,
        rentFreeBeginDate: null,
        rentFreeEndDate: null,
        rentBeginDate: null,
        rentEndDate: null,
        nextRentDate: null,
        officeFeeId: null,
        createBy: null,
        apartmentFeeId: null,
        createTime: null,
        updateBy: null,
        propertyFeeId: null,
        updateTime: null,
        facilityFeeId: null,
        fixedFeeId: null,
        remark: null,
        parkingFeeId: null,
        delFlag: null,
        recordType: null,
        elevatorFeeId: null,
        recordState: null,
        effect: null,
        roomIds: null,
        calculationMode: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.enterpriseId = null
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    // 修改生效状态
    submitEffect() {
      if (this.form.id != null) {
        var req = {
          id: this.form.id,
          effect: this.form.effect,
          calculationMode: this.form.calculationMode,
        }
        updateEffect(req).then(() => {
          this.msgSuccess('修改成功')
          this.effectOpen = false
          this.getList()
        })
      }
    },

    submitBill() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            if (null != this.form.billDate && '' != this.form.billDate) {
              this.form.billBeginDate = this.form.billDate[0],
              this.form.billEndDate = this.form.billDate[1]
            }
            var req = {
              id: this.form.id,
              billMoney: this.form.billMoney,
              billBeginDate: this.form.billBeginDate,
              billEndDate: this.form.billEndDate,
              billType: this.form.billType,
              billNextDay: this.form.billNextDay,
              billDeadline: this.form.billDeadline
            }
            createBill(req).then(() => {
              this.msgSuccess('账单已生成')
              this.billOpen = false,
                this.getList()
            })
          }
        }
      })

    },

    handleBill(row) {
      this.reset()
      const id = row.id || this.ids
      if (row.calculationMode == '手动计费'){
        getFeLeaseRecord(id).then(response => {
          this.form = response.data
          this.billOpen = true
          this.title = '手动生成账单'
        })
      }else {
        this.msgError('当前账单为自动计费,不能手动生成账单')
      }
    },

    handleEffect(row) {
      this.reset()
      const id = row.id || this.ids
      getFeLeaseRecord(id).then(response => {
        this.form = response.data
        this.form.effect=this.form.effect+''

        this.effectOpen = true
        this.title = '修改租赁生效状态'
      })
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加租赁记录'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        name: 'LeasingDetail',
        query: { id: row.id }
      })
    },
    handleEdit(row) {
      this.$router.push({
        name: 'LeasingEdit',
        query: { id: row.id }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateFeLeaseRecord(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addFeLeaseRecord(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm(
        '是否确认删除租赁记录编号的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return delFeLeaseRecord(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'life/feLeaseRecord/export',
        {
          ...this.queryParams
        },
        `life_feLeaseRecord.xlsx`
      )
    }
  }
}
</script>
